<?php
namespace app\admin\controller; 
use think\AjaxPage;
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
class Storeb extends Base {
	
	
	public function index(){
		return $this->fetch();
	}
	
	public function ajaxindexList(){
		
		$storeid = session('store_id');		
//		exit( '$storeid='.$storeid);	
		$where = " storeid= $storeid "; // 搜索条件                
        if(I('isshow') !== '') {
        	$where = "$where and isshow = ".I('isshow');
		}                

        // 关键词搜索               
        $key_word = I('key_word') ? trim(I('key_word')) : '';
        if($key_word)
        {
            $where = "$where and name like '%$key_word%'" ;
        }
        
        $count = M('beautician')->where($where)->count();
        $Page  = new AjaxPage($count,15);

        $show = $Page->show();
		
		if(I('orderby1')!=''){
			$order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
		}
		else{
			$order_str = "beautician_id desc";
		}
        
        $bList = M('beautician')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();
		
		//M('ad_type')->where(['storeid'=>$storeid])->getField("atypeid,atypename");   
		$rList = M('beautician_role')->where(['storeid'=>$storeid])->getField("id,rolename");

        $this->assign('bList',$bList);
		$this->assign('rList',$rList);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
	}

	function addEditb(){
		
		$beautician_id = I('beautician_id/d',0);
        $type = $beautician_id > 0 ? 2 : 1; // 标识自动验证时的 场景 1 表示插入 2 表示更新
        $storeid = session('store_id');	
        //ajax提交验证
        if (IS_POST) {
            // 数据验证
            $data = input('post.');
            $validate = \think\Loader::validate('StoreBeautician');
            if (!$validate->batch()->check($data)) {
                $error = $validate->getError();
                $error_msg = array_values($error);
                $return_arr = array(
                    'status' => -1,
                    'msg' => $error_msg[0],
                    'data' => $error,
                );
                $this->ajaxReturn($return_arr);
            }
			

            if ($type == 2) {
				
            	M('beautician')->where("beautician_id=$beautician_id")->save($data);
            } else {
            	$data['times'] = time();
				$data['isshow']=1;			
				$data['storeid'] = $storeid;				
				M('beautician')->add($data);
                $beautician_id = M('beautician')->getLastInsID();
            }
			
            $return_arr = array(
                'status' => 1,
                'msg' => '操作成功',
                'data' => array('url' => U('Admin/Storeb/index')),
            );
            $this->ajaxReturn($return_arr);
        }

        $bInfo = Db::name('beautician')->where('beautician_id=' . I('GET.beautician_id', 0))->find();
		
		$rtypeinfo = Db::name('beautician_role')->where(['storeid'=>$storeid])->select();
        $this->assign('info', $bInfo);
		$this->assign('rtpinfo', $rtypeinfo);
		return $this->fetch('addEditb');
	}

	function delb(){
		$ids = I('post.ids','');
        empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        $goods_ids = rtrim($ids,",");

		
        // 删除此技师        
        M("beautician")->whereIn('beautician_id',$goods_ids)->delete();  //

        $this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>U("Admin/Storeb/index")]);
	}
	
	
	//角色列表
	function rolelist(){
		return $this->fetch();
	}
	
	//角色列表
	function ajaxrole(){
		$storeid = session('store_id');			
		$where = " storeid= $storeid "; // 搜索条件                              

        $count = M('beautician_role')->where($where)->count();
        $Page  = new AjaxPage($count,20);

        $show = $Page->show();	
		$order_str = "id desc";       
        $rList = M('beautician_role')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('rolelist',$rList);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
	}
	
	
	//添加或者编辑角色
	function addeditrole(){
		$id = I('id/d',0);
		
		if ((I('is_ajax') == 1) && IS_POST) {
			$rolename = I('rolename/s','');
			
			if($rolename == '' || $rolename == null){
				$this->ajaxReturn(['status' => -1,'msg' => '','data' =>['rolename'=>'职务名称必填']]);
			}
			
			$data = input('post.');
			if($id <= 0){
				$storeid = session('store_id');	
				$data['storeid'] = 	$storeid;	
				M('beautician_role')->add($data);
			}
			else{
				M('beautician_role')->where(['id'=>$id])->save($data);
			}
			$this->ajaxReturn(['status' => 1,'msg' => '操作成功','data' =>['url'=>U("Admin/Storeb/rolelist")]]);
		}
		
		$rinfo = Db::name('beautician_role')->where('id=' . I('GET.id', 0))->find();
        $this->assign('rinfo', $rinfo);  // 商品详情
        return $this->fetch();
	}
	
	//删除角色
	function delrole(){
		$ids = I('post.ids','');
        empty($ids) &&  $this->ajaxReturn(['status' => -1,'msg' =>"非法操作！",'data'  =>'']);
        $goods_ids = rtrim($ids,",");
		
		$aresult = M("beautician")->whereIn('role',$ids)->select();
		if(count($aresult)>0){
			$this->ajaxReturn(['status' => -1,'msg' =>"已有美体师使用此职务,请先更改对应美体师职务！",'data'  =>'']);
		}
		
        // 删除此角色      
        M("beautician_role")->whereIn('id',$goods_ids)->delete();
		$this->ajaxReturn(['status' => 1,'msg' => '操作成功','url'=>U("Admin/Storeb/rolelist")]);  
	}
	
}
?>